package com.roy.parser;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import com.roy.bean.Stock;

public class AAstock extends Parser {

	@Override
	public Stock parse(String code) {
		try {
			Stock stock = new Stock();
			URL url = new URL(
					"http://www.aastocks.com/pkages/web/bankcomm/chi/whatshot/quote.asp?symbol="
							+ code);
			BufferedReader in = new BufferedReader(new InputStreamReader(url
					.openConnection().getInputStream()));
			String htmlResult = "";
			String inputLine;

			while ((inputLine = in.readLine()) != null)
				htmlResult += inputLine;

			String buffer;

			if (htmlResult.indexOf("info@aastocks.com") > 0) {
				System.out.println("stock #" + code + " is not validate");
				return null;
			} else {

				stock.setSymbol(code);

				stock.setName(htmlResult.substring(htmlResult
						.indexOf("<strong>") + 8, htmlResult
						.indexOf("</strong>")));

				htmlResult = htmlResult.substring(htmlResult
						.indexOf("</strong>") + 1);

				stock.setPrice(Double.valueOf(htmlResult.substring(htmlResult
						.indexOf("<strong>") + 8, htmlResult
						.indexOf("</strong>"))));

				htmlResult = htmlResult.substring(htmlResult
						.indexOf("</strong>") + 1);

				buffer = htmlResult;
				buffer = htmlResult.substring(
						htmlResult.indexOf("<strong>") + 8, htmlResult
								.indexOf("</strong>"));

				stock.setStatus(Stock.Status.STAY);
				if (buffer.indexOf("stock_up") >= 0) {
					stock.setStatus(Stock.Status.UP);
				}
				if (buffer.indexOf("stock_down") >= 0) {
					stock.setStatus(Stock.Status.DOWN);
				}

				buffer = buffer.substring(buffer.indexOf(";") + 1);
				stock.setChange(Double.valueOf(buffer));

				htmlResult = htmlResult.substring(htmlResult
						.indexOf("</strong>") + 1);

				stock.setBid(Double.valueOf(htmlResult.substring(htmlResult
						.indexOf("<strong>") + 8, htmlResult
						.indexOf("</strong>"))));

				htmlResult = htmlResult.substring(htmlResult
						.indexOf("</strong>") + 1);

				buffer = htmlResult.substring(
						htmlResult.indexOf("<strong>") + 8, htmlResult
								.indexOf("</strong>"));
				if (!buffer.equalsIgnoreCase("N/A")) {
					stock.setRangeDayHigh(Double.valueOf(htmlResult.substring(
							htmlResult.indexOf("<strong>") + 8, htmlResult
									.indexOf("</strong>"))));
				}
				;
				htmlResult = htmlResult.substring(htmlResult
						.indexOf("</strong>") + 1);
				buffer = htmlResult.substring(
						htmlResult.indexOf("<strong>") + 8, htmlResult
								.indexOf("</strong>"));
				if (!buffer.equalsIgnoreCase("N/A")) {

					stock.setOpenPrice(Double.valueOf(htmlResult.substring(
							htmlResult.indexOf("<strong>") + 8, htmlResult
									.indexOf("</strong>"))));
				}
				htmlResult = htmlResult.substring(htmlResult
						.indexOf("</strong>") + 1);

				htmlResult = htmlResult.substring(htmlResult.indexOf(";") + 1);

				stock.setChangeRate(Double.valueOf(htmlResult.substring(0,
						htmlResult.indexOf("\t"))));

				htmlResult = htmlResult.substring(htmlResult
						.indexOf("</strong>") + 1);

				stock.setAsk(Double.valueOf(htmlResult.substring(htmlResult
						.indexOf("<strong>") + 8, htmlResult
						.indexOf("</strong>"))));

				htmlResult = htmlResult.substring(htmlResult
						.indexOf("</strong>") + 1);
				buffer = htmlResult.substring(
						htmlResult.indexOf("<strong>") + 8, htmlResult
								.indexOf("</strong>"));
				if (!buffer.equalsIgnoreCase("N/A")) {

					stock.setRangeDayLow(Double.valueOf(htmlResult.substring(
							htmlResult.indexOf("<strong>") + 8, htmlResult
									.indexOf("</strong>"))));
				}
				htmlResult = htmlResult.substring(htmlResult
						.indexOf("</strong>") + 1);

				stock.setLastTrade(Double.valueOf(htmlResult.substring(
						htmlResult.indexOf("<strong>") + 8, htmlResult
								.indexOf("</strong>"))));

				htmlResult = htmlResult
						.substring(htmlResult.indexOf("#EDEDED") + 1);

				// stock.setVolume(volume);

				htmlResult = htmlResult
						.substring(htmlResult.indexOf("#EDEDED") + 1);

				// stock.setVolumePrice(volumePrice);

				htmlResult = htmlResult
						.substring(htmlResult.indexOf("#EDEDED") + 1);

				stock
						.setRange52High(Double.valueOf(htmlResult.substring(
								htmlResult.indexOf(">") + 1, htmlResult
										.indexOf(" -"))));

				stock
						.setRange52Low(Double.valueOf(htmlResult.substring(
								htmlResult.indexOf("- ") + 2, htmlResult
										.indexOf("<"))));

				htmlResult = htmlResult
						.substring(htmlResult.indexOf("#EDEDED") + 1);

				htmlResult = htmlResult
						.substring(htmlResult.indexOf("#EDEDED") + 1);
				buffer = htmlResult.substring(htmlResult.indexOf(">") + 1,
						htmlResult.indexOf("<"));
				if (!buffer.equalsIgnoreCase("N/A")) {

					stock.setPE(Double.valueOf(htmlResult.substring(htmlResult
							.indexOf(">") + 1, htmlResult.indexOf("<"))));
				} else {
					stock.setPE(0);
				}
				return stock;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
